package br.com.fidalgotecnologia.layar.converter;

import java.util.StringTokenizer;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import br.com.fidalgotecnologia.layar.entity.Layer;

@FacesConverter(value="LayerConverter")
public class LayarConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext facesCTX, UIComponent component, String value) {
		 if (value == null || (value.trim().length() == 0))
	        {
	            return value;
	        }

	        Layer layer = new Layer();
	        boolean conversionError = false;

	        int hyphenCount = 0;
	        StringTokenizer hyphenTokenizer = new StringTokenizer(value, "-");
	        while (hyphenTokenizer.hasMoreTokens())
	        {
	            String token = hyphenTokenizer.nextToken();
	            try
	            {
	                if (hyphenCount == 0)
	                {   
	                	layer.setId(Long.parseLong(token));
	                }

	                if (hyphenCount == 1)
	                {
	                	layer.setLayer(token);
	                }

	                if (hyphenCount == 2)
	                {
	                	layer.setErrorCode(Integer.parseInt(token));
	                }
	                if (hyphenCount == 3)
	                {
	                	layer.setErrorString(token);
	                }
	                if (hyphenCount == 4)
	                {
	                	layer.setNextPageKey(token);
	                }
	                if (hyphenCount == 5)
	                {
	                	layer.setMorePages(Boolean.valueOf(token));
	                }
	                if (hyphenCount == 6)
	                {
	                	layer.setRadius(Long.parseLong(token));
	                }
	                if (hyphenCount == 7)
	                {
	                	layer.setRefreshInterval(Integer.parseInt(token));
	                }
	                if (hyphenCount == 8)
	                {
	                	layer.setRefreshDistance(Integer.parseInt(token));
	                }
	                if (hyphenCount == 9)
	                {
	                	layer.setFullRefresh(Boolean.valueOf(token));
	                }
	                if (hyphenCount == 10)
	                {
	                	layer.setShowMessage(token);
	                }
	                if (hyphenCount == 11)
	                {
	                	layer.setDisableClueMenu(Boolean.valueOf(token));
	                }
	                if (hyphenCount == 12)
	                {
	                    layer.setBiwStyle(token);
	                }
	                
	                hyphenCount ++;       
	            }
	            catch (Exception exception)
	            {
	                conversionError = true;
	            }
	        }

	        if (conversionError || (hyphenCount != 3))
	        {
	            throw new ConverterException();
	        }

	        return layer;
	    }

	   

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		// TODO Auto-generated method stub
		return null;
	}

}
